<?php

namespace app\recode\controller;

use \app\check\controller\Check;
use app\common\model\RecodeNote;
use think\Cache;
use think\Db;

// 星盘档案管理控制器
class Note extends Check
{
    /**
     * 添加笔记注释
     */
    public function add()
    {
        $content = input('content/s', '', 'htmlspecialchars');
        $rid = input('rid/d');
        $time = time();
        $event_time = input('event_time/d', 0);
        $from = input('from/s', '', 'htmlspecialchars');
        $uid = $this->user['id'];
        $data = [
            'uid' => $uid,
            'content' => $content,
            'rid' => $rid,
            'from' => $from,
            'create_time' => $time,
            'last_time' => $time,
            'event_time' => $event_time
        ];
        if (!$uid)
        {
            return json(['code' => 10401, 'User is not exist.']);
        }
        if (!$rid)
        {
            return json(['code' => 10402, 'Recode is not exist.']);
        }
        if (trim($content) == '')
        {
            return json(['code' => 10403, 'Content can not be empty.']);
        }
        $r_data = Db::table('app_recode')->find($rid);
        if (empty($r_data) || $r_data['uid'] != $this->user['id'])
        {
            return json(['code' => 10404, 'msg' => 'Recode is not exist.']);
        }
        $data['id'] = Db::table('app_recode_note')->insertGetId($data);

        Cache::rm(RecodeNote::REDIS_KEY_IDS . $rid);
        if (!$data['id'])
        {
            return json(['code' => 10405, 'msg' => 'Failed.']);
        }
        return json(['code' => 200, 'data' => $data, 'msg' => 'Success.']);
    }

    /**
     * 修改笔记注释
     */
    public function edit()
    {
        $id = (input('id/d', 0));
        $content = htmlspecialchars(input('content'));
        $rid = (input('rid/d'));
        $time = time();
        $data = [
            'content' => $content,
            'last_time' => $time,
        ];
        if (trim($content) == '')
        {
            return json(['code' => 10407, 'Content can not be empty.']);
        }
        $uid = $this->user['id'];

        $r_data = Db::table('app_recode')->find($rid);
        if (empty($r_data) || $r_data['uid'] != $this->user['id'])
        {
            return json(['code' => 10408, 'msg' => 'Recode is not exist.']);
        }
        $n_data = Db::table('app_recode_note')->where(['id' => $id, 'uid' => $uid])->find();
        if (empty($n_data))
        {
            return json(['code' => 10409, 'msg' => 'Recode Note is not exist.']);
        }
        Db::table('app_recode_note')->where(['id' => $id, 'uid' => $uid])->update($data);
        Cache::rm(RecodeNote::REDIS_KEY_DETAIL . $id);

        return json(['code' => 200, 'data' => $data, 'msg' => 'Success.']);
    }

    /**
     * 删除注释
     */
    public function del()
    {
        $id = input('id/d');

        $uid = $this->user['id'];

//        Db::table('app_recode_note')->where(['id' => $id, 'uid' => $uid])->delete();

        (new \app\common\cachemodel\RecodeNote())->DelData($this->user['id'], $id);

        (new RecodeNote())->del($id);//删除缓存

        return json(['code' => 200, 'msg' => 'Delete Success.']);

//        $n_data = Db::table('app_recode_note')->where(['id'=>$id])->find();
//        if(empty($n_data))
//        {
//            return json(['code'=>10411,'msg'=>'Recode Note is not exist.']);
//        }
//        $uid = $this->user['id'];
//        if($uid != $n_data['uid'])
//        {
//            return json(['code'=>10412,'msg'=>'You are not the note owner.']);
//        }
//        if(Db::table('app_recode_note')->where(['id'=>$id,'uid'=>$uid])->delete())
//        {
//            return json(['code'=>200,'msg'=>'Delete Success.']);
//        }
//        return json(['code'=>10413,'msg'=>'Delete Failed.']);
    }

    /**
     * 查询该档案下所有笔记注释
     */
    public function lists()
    {
        $rid = input('rid/d');
        $uid = $this->user['id'];

        if (!$rid)
        {
            return json(['code' => 10414, 'msg' => 'Recode is not exist.']);
        }
        if (!$uid)
        {
            return json(['code' => 10415, 'msg' => 'User is not exist..']);
        }

        $is_use_cache = 1;
        if ($is_use_cache)
        {
            $data = (new RecodeNote())->getListAll(['rid' => $rid, 'uid' => $uid]);
        } else
        {
            $data = Db::table('app_recode_note')->where(['rid' => $rid, 'uid' => $uid])->order('create_time DESC')->select();
        }
        return json(['code' => 200, 'data' => $data, 'msg' => 'Success.']);
    }

    /**
     * 统计注释的数量
     * @param $rid int 该档案下注释的数量
     * @return \think\response\Json
     * @throws \think\Exception
     */
    public function counts()
    {
        $rid = input('param.rid/d', 0);
        if (!$rid)
        {
//            \think\Log::write($this->user['id'] . '统计注释数量错误：' . var_export($this->headers, true), 'error');
            return show(config('code.error'), '未知的档案');
        }
        $uid = (int)$this->user['id'];

        $is_use_cache = 1;
        $reocdeModel = new \app\common\cachemodel\Recode();
        if ($is_use_cache)
        {
            $count = (new RecodeNote())->getCountAll(['rid' => $rid, 'uid' => $uid]);
            $info = $reocdeModel->getInfoById($rid);
            if (isset($info['is_read']) && ($info['is_read'] === '0' || $info['is_read'] === 0))
            {
                $reocdeModel->UpdateData($uid, $rid, ['is_read' => 1]);
            }
        } else
        {
            $count = Db::table('app_recode_note')->where("rid=$rid AND uid=$uid")->count('id');
            $reocdeModel->UpdateData($uid, $rid, ['is_read' => 1]);
        }
        return json(['code' => 200, 'data' => $count, 'msg' => 'Success.']);
    }
}
